define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'product/index' + location.search,
                    add_url: 'product/add',
                    edit_url: 'product/edit',
                    del_url: 'product/del',
                    multi_url: 'product/multi',
                    import_url: 'product/import',
                    table: 'product',
                    dragsort_url:'',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                search:false,
                showToggle: false,
                showColumns: false,
                showExport: false,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'qiye_id', title: __('Qiye_id')},
                        {field: 'randomid', title: __('Randomid'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'name', title: __('Name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'images', title: __('Images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'chanpinxinghao', title: __('Chanpinxinghao'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'category_id', title: __('Category_id')},
                        {field: 'biaoji', title: __('Biaoji'), searchList: {"0":__('Biaoji 0'),"1":__('Biaoji 1'),"2":__('Biaoji 2')}, formatter: Table.api.formatter.normal},
                        {field: 'status', title: __('Status'), searchList: {"-1":__('Status -1'),"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'category.name', title: __('Category.name'), operate: 'LIKE'},
                        {field: 'qiye.name', title: __('Qiye.name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            $('#btn_youzhi').click(function(){
                var temp=Table.api.selectedids($("#table"));
                Fast.api.ajax({
                  url: 'product/set_youzhi',
                  data: {"ids": temp},
                 }, function (data, ret) {
                     console.log(ret);
                   if(ret.code == 1){
                     Layer.closeAll();
                     Toastr.success(ret.msg);
                     $('.btn-refresh').trigger('click');
                    }else{
                        Layer.alert(ret.msg);
                  }
                   return false;   //这里很关键，如果不加会弹出两次提示
                }, function (data, ret) {
                   Layer.alert(ret.msg);
                 });
            });
            $('#btn_chuangxin').click(function(){
                var temp=Table.api.selectedids($("#table"));
                Fast.api.ajax({
                  url: 'product/set_chuangxin',
                  data: {"ids": temp},
                 }, function (data, ret) {
                     console.log(ret);
                   if(ret.code == 1){
                     Layer.closeAll();
                     Toastr.success(ret.msg);
                     $('.btn-refresh').trigger('click');
                    }else{
                        Layer.alert(ret.msg);
                  }
                   return false;   //这里很关键，如果不加会弹出两次提示
                }, function (data, ret) {
                   Layer.alert(ret.msg);
                 });
            });
            $('#btn_cancel').click(function(){
                var temp=Table.api.selectedids($("#table"));
                Fast.api.ajax({
                  url: 'product/set_cancel',
                  data: {"ids": temp},
                 }, function (data, ret) {
                     console.log(ret);
                   if(ret.code == 1){
                     Layer.closeAll();
                     Toastr.success(ret.msg);
                     $('.btn-refresh').trigger('click');
                    }else{
                        Layer.alert(ret.msg);
                  }
                   return false;   //这里很关键，如果不加会弹出两次提示
                }, function (data, ret) {
                   Layer.alert(ret.msg);
                 });
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
